package gui;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import common.Logger;

import server.Server;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ServerMainWindow implements Observer {

	private JFrame frmServer;
	private JTextField textField;
	private JTextArea textArea;
	private Server server;
	
	public void close() {
		//frmServer.action(WindowEvent.WINDOW_CLOSING, what);
		//processWindowEvent( new WindowEvent( frmServer, WindowEvent.WINDOW_CLOSING) );
		frmServer.dispose();
	}
	
	public static void openWindow(final Server server) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerMainWindow window = new ServerMainWindow(server);
					window.frmServer.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		openWindow(null);
	}
	
	private void runCommand() {
		
		server.log.log(" -> " + textField.getText());
		server.command(textField.getText());
		textField.setText("");
	}
	
	/**
	 * Create the application.
	 */
	public ServerMainWindow(Server server) {
		this.server = server;
		server.log.addObserver(this);
		initialize();
		server.windowInitialized(this);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmServer = new JFrame();
		frmServer.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				server.close();
			}
		});
		frmServer.setTitle("Server");
		frmServer.setBounds(100, 100, 523, 460);
		frmServer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setDividerSize(0);
		splitPane.setResizeWeight(1.0);
		frmServer.getContentPane().add(splitPane, BorderLayout.SOUTH);
		
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					runCommand();
				}
			}
		});
		
		splitPane.setLeftComponent(textField);
		textField.setColumns(10);
		
		JSplitPane splitPane_1 = new JSplitPane();
		splitPane_1.setDividerSize(0);
		splitPane_1.setResizeWeight(0.5);
		splitPane.setRightComponent(splitPane_1);
		
		JButton btnExecute = new JButton("Execute");
		btnExecute.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				runCommand();
			}
		});
		splitPane_1.setLeftComponent(btnExecute);
		
		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				server.log.clear();
			}
		});
		splitPane_1.setRightComponent(btnClear);
		
		JScrollPane scrollPane = new JScrollPane();
		frmServer.getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		textArea = new JTextArea();
		textArea.setTabSize(4);
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		scrollPane.setViewportView(textArea);
		
		centerWindow();
	}

	@Override
	public void update(Observable o, Object arg) {
		textArea.setText(((Logger)arg).getLogs());
		textArea.setCaretPosition(textArea.getDocument().getLength());
	}
	
	
	private void centerWindow() {
		Toolkit tk = Toolkit.getDefaultToolkit();
	    Dimension screenSize = tk.getScreenSize();
	    int screenHeight = screenSize.height;
	    int screenWidth = screenSize.width;
	    //frmServer.setSize(screenWidth / 2, screenHeight / 2);
	    frmServer.setLocation(	(screenWidth - frmServer.getWidth()) / 2,
	    						(screenHeight - frmServer.getHeight()) / 2);
	}

}
